package com.servlet;

import com.bean.BhUS;
import com.bean.BhUser;
import com.bean.BhWorkData;
import com.dao.Studentworkdao;
import com.dao.impl.Studentworkdapimpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@WebServlet("/stcx/*")
public class StcxServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;
    Studentworkdao st=new Studentworkdapimpl();
    Gson gson=new Gson();
    //查地区
    protected void region(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setContentType("text/html");
        response.setContentType("application/json");
        PrintWriter out=response.getWriter();
        HttpSession session=request.getSession();
        BhUser bh = (BhUser) session.getAttribute("BhSyUser");
        try {
            List num=new ArrayList();
            List sum=Collections.singletonList(0);
            List<BhWorkData> list=st.sely(bh.uID);
            for (int j = 0; j < list.size() ; j++) {
                //System.out.println(list.get(j).getMonth());
                sum= Collections.singletonList(list.get(j).getMonth());
                num.addAll(sum);
            }
            String s=gson.toJson(num);
            out.print(s);//放回信息
            //System.out.println("s"+s);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }

    protected void selt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType( "text/html;charset=utf-8" );
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        String y = request.getParameter("y");
        System.out.println(y);
        int t=Integer.parseInt(y);
        System.out.println(t);
        HttpSession session=request.getSession();
        BhUser bh = (BhUser) session.getAttribute("BhSyUser");
        try {

            List num=new ArrayList();
            List sum=Collections.singletonList(0);
            List<BhWorkData> list=st.selt(bh.uID,t);
            for (int j = 0; j < list.size() ; j++) {
                //System.out.println(list.get(j).getMonth());
                sum= Collections.singletonList(list.get(j).getDay());
                num.addAll(sum);
            }
            String ok = gson.toJson(num);
            out.print(ok);//返回信息
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        out.flush();
        out.close();
    }
    protected void selxx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType( "text/html;charset=utf-8" );
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        String y=request.getParameter("y");
        String t=request.getParameter("t");
        int y1=Integer.parseInt(y);
        int t1=Integer.parseInt(t);
        System.out.println(y1);
        System.out.println(t1);
        HttpSession session=request.getSession();
        BhUser bh = (BhUser) session.getAttribute("BhSyUser");
        try {

//            List num=new ArrayList();
//            List sum=Collections.singletonList(0);
            List<BhUS> list=st.selxx(bh.uID,bh.uClass,y1,t1);
           // List<BhUser> listBu = st.selname(list);

//            for (int j = 0; j < list.size() ; j++) {
//                //System.out.println(list.get(j).getMonth());
//                sum= Collections.singletonList(list.get(j).getDay());
//                num.addAll(sum);
//            }
            System.out.println(list);
            //System.out.println(listBu);
            String ok = gson.toJson(list);
            //String okbu = gson.toJson(listBu);
            out.print(ok);//返回信息
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }

}
